package com.fs.controller;

import com.fs.common.Result;
import com.fs.pojo.User;
import com.fs.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController {
     //注入UsersService接口
    @Autowired
    private UserService userService;
    //登录方法
    @RequestMapping("/login")
    //登录成功 {status:"200",msg:"success",data:{}}
    //失败    {status:"500",msg:"fail",data:null}
    public Result login(String username, String password){
        //@RequestBody作用 将json字符串转成Java对象
        //让程序睡5秒
        try {
            Thread.sleep(5000);//alt+enter 纠错
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        User user =  userService.login(username,password);
        System.out.println(user);
      if(user ==null){
          //登录失败
          return Result.format("用户名或者密码错误!");
      }else{//登录成功 返回Users对象
          return Result.ok(user);
      }
    }

    //注销登录
    @RequestMapping("/login/logout")
    public Result logout(){
        return Result.build(200,"注销成功！");
    }

    //注册
    @RequestMapping("/regist")
    public Result regist(String username, String password){
        try {
            Thread.sleep(5000);//alt+enter 纠错
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        User user = userService.regist(username, password);
        System.out.println(user);
        if(user ==null){
            //注册失败
            return Result.format("注册失败!");
        }else{//注册成功 返回User对象
            return Result.ok(user);
        }
    }

    //获取ID
    @RequestMapping("/findById")
    public User findByid(String userId){
        Integer id = Integer.valueOf(userId);
        System.out.println(id);
        User user= userService.findById(id);
        return user;
    }


    //查询所有的用户信息  快捷键 alt+enter 纠错
    @RequestMapping("/findAll")
    public List<User> findAll(){
        //使用工具自动生成方法
        List<User> users= userService.findAll();
        return users;
    }
}
